package com.mbbmap.app.manager;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;

import com.mbbmap.util.EnhancedException;


/**
 * This class is a Singleton that provides access to systemConfig parameters
 * defined in a Properties file <i>system.properties</i>. A client gets access to the single instance
 * through the static {@link ConfigManager#getInstance() getInstance} method.
 * <p>
 *
 * The properties can be accessed using the
 * {@link ConfigManager#get(java.lang.String) get} method,
 * passing in the appropriate key as a parameter. Null is returned if the
 * key does not exist.
 */

public class ConfigManager {
	private static Properties systemConfig = null;
	private static Hashtable props = null;
	private static ConfigManager instance = null;

	private static int TIMEOUT = 10000;  //DB connection timeout if unable to
										 //get connection in less than 10 seconds
	//private DBConnectionManager connMgr;
	private String connectionPool;

	private final static String SYSTEM_PROPERTY_FILE = "/system.properties";

	private ConfigManager() {
		super();
		//connMgr = DBConnectionManager.getInstance();
		String timeOutValue = get("dbWaitTime");
		if (timeOutValue != null) {
			try {
				TIMEOUT = Integer.parseInt(timeOutValue);
			} catch (NumberFormatException e) {}
		}
	}

	/**
	 * Gets the connection pool for this application
	 *
	 * @return The connection pool. Defined in system properties file under connection.pool
	 */
	public String getConnectionPool() {
		return connectionPool;
	}

	public void setConnectionPool(String pool) {
		this.connectionPool = pool;
	}

    /**
     * Returns the single instance, creating one if it's the
     * first time this method is called.
     *
     * @return ConfigManager. The single instance.
     */
	public static ConfigManager getInstance() {
		//thread safe implementation - double-check idiom
		if (instance == null) {
			synchronized (ConfigManager.class) {
				if (instance == null) {
					instance = new ConfigManager();
					instance.loadSystemProperties();
					instance.connectionPool = instance.get("connection.pool");
					//instance.verifyORMapping();

					//override pool using local environment property 'connectionPool'
					String localPool = instance.get("connection.pool");
System.out.println("localPool is " + localPool);
					if (localPool != null) {
						System.out.println("Setting connection pool to " + localPool);
						instance.setConnectionPool(localPool);
					}
				}
			}
		}
		return instance;
	}

	/**
	 * Loads the ConfigManager with the systemConfig parameters.
	 *
	 */
	public void loadSystemProperties() {
		InputStream is = getClass().getResourceAsStream(SYSTEM_PROPERTY_FILE);
		systemConfig = new Properties();
		try {
			systemConfig.load(is);
		} catch (IOException e) {
			EnhancedException ex = new EnhancedException(e.getMessage());
			ex.setExtraMessage("Can't read properties file. Make sure " + SYSTEM_PROPERTY_FILE + " is in the CLASSPATH");
			ex.setExceptionLocation("ConfigManager_loadSystemProperties_001");
			SystemLogManager.getInstance().logError(ex);
		}
	}

	/**
	 * Retrieves the current config properties object
	 *
	 * @return The Properties object that contains the systemConfig.
	 */
	public Properties getSystemProperties() {
		return this.systemConfig;
	}

	/**
	 * Retrieves the value of a given systemConfig entry.
	 *
	 * @param key The name of an entry.
	 *
	 * @return String The corresponding value. <code>null</code> is returned
	 * 				  if the entry does not exist.
	 */
	public String get(String key) {
		if (systemConfig != null) {
			return systemConfig.getProperty(key);
		} else {
			return null;
		}
	}

}
